package cn.e4j.modular.crm.service.impl;


import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwbzT;
import cn.e4j.modular.crm.mapper.GwbzTMapper;
import cn.e4j.modular.crm.service.GwbzTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  客户黑名单 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-12-20
 */
@Service
public class GwbzTServiceImpl extends ServiceImpl<GwbzTMapper, GwbzT> implements GwbzTService {

    @Override
    public List<Map<String, Object>> getGwbzList(Page<Object> page, Dto inDto) {
        List<Map<String, Object>> list = this.baseMapper.getGwbzList(page, inDto);
        return list;
    }

    @Override
    public Boolean existedTheParams(GwbzT gwbzT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        QueryWrapper<GwbzT> gwbzTQueryWrapper = new QueryWrapper<>();
        gwbzTQueryWrapper.eq("GWBZ001", gwbzT.getGWBZ001());
        gwbzTQueryWrapper.eq("GWBZENT", shiroUser.getDBC());
        return this.baseMapper.selectList(gwbzTQueryWrapper).size() > 0;
    }

    @Override
    @Transactional
    public void add(GwbzT gwbzT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbzT.setGWBZENT(shiroUser.getDBC());
        gwbzT.insert();
    }

    @Override
    @Transactional
    public void edit(GwbzT gwbzT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbzT.setGWBZMODU(shiroUser.getID());
        UpdateWrapper<GwbzT> gwbzTUueryWrapper = new UpdateWrapper<>();
        gwbzTUueryWrapper.eq("GWBZ001", gwbzT.getGWBZ001());
        gwbzTUueryWrapper.eq("GWBZENT", shiroUser.getDBC());
        gwbzT.update(gwbzTUueryWrapper);
    }

    @Override
    @Transactional
    public void delete(GwbzT gwbzT) {
        UpdateWrapper<GwbzT> gwbzTQueryWrapper = new UpdateWrapper<>();
        gwbzTQueryWrapper.eq("GWBZENT", gwbzT.getGWBZENT());
        gwbzTQueryWrapper.eq("GWBZ001", gwbzT.getGWBZ001());
        gwbzT.delete(gwbzTQueryWrapper);
    }

    @Override
    @Transactional
    public void deleteBatch(List<GwbzT> gwbzTList) {
        for (GwbzT gwbzT : gwbzTList) {
            UpdateWrapper<GwbzT> gwbzTQueryWrapper = new UpdateWrapper<>();
            gwbzTQueryWrapper.eq("GWBZENT", gwbzT.getGWBZENT());
            gwbzTQueryWrapper.eq("GWBZ001", gwbzT.getGWBZ001());
            gwbzT.delete(gwbzTQueryWrapper);
        }
    }

    @Override
    @Transactional
    public void updateStatus(List<GwbzT> gwbzTList) {
        for (GwbzT gwbzT : gwbzTList) {
            UpdateWrapper<GwbzT> gwbzTQueryWrapper = new UpdateWrapper<>();
            gwbzTQueryWrapper.eq("GWBZENT", gwbzT.getGWBZENT());
            gwbzTQueryWrapper.eq("GWBZ001", gwbzT.getGWBZ001());
            gwbzT.update(gwbzTQueryWrapper);
        }
    }
}
